package com.unytech.watersoil.action.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Controller;

import com.unytech.watersoil.action.BaseAction;
import com.unytech.watersoil.common.GlobalEnum;
import com.unytech.watersoil.entity.security.Permission;

@Controller("/security/permission_getPermission")
public class PermissionAction extends BaseAction{
	private String parentid;
	
	/**
	 * 取得SESSION中当前父菜单下的所有子菜单
	 * @return AJAX调用此方法，无须跳转，返回以字符串形式返回XML数据
	 */
	public String getSessionPermission(){
		List<Permission> allpermission = (List<Permission>) getSession().get(GlobalEnum.SESSIONPERMISSION.toString());		
		List<Permission> sonpermission = new ArrayList<Permission>();
		
		System.out.println("用jQuery AJAX请求二级菜单时传过来的父菜单ID:"+this.parentid.trim());
		
		for (int i=0; i<allpermission.size(); i++)
		{
			if (allpermission.get(i).getParentId().trim().equals(this.parentid.trim()))
			{
				//判断当前子权限菜单是有自己的子权限菜单（非选项卡和按钮），有权限子菜单和没有子权限菜单在前台显示是不同的，故后台要传送标识
				Set<Permission> sonsonset = new HashSet<Permission>();
				for (int j=0; j<allpermission.size(); j++)
				{
					if (allpermission.get(j).getPermType().equals(GlobalEnum.ACCORDION) && allpermission.get(j).getParentId().equals(allpermission.get(i).getPermid()))
					{
						sonsonset.add(allpermission.get(i));
					}
				}
				allpermission.get(i).setSubPerms(sonsonset);
				sonpermission.add(allpermission.get(i));
			}
		}
		
		 getResponse().setContentType("text/xml;charset=utf-8"); 
		 getResponse().setCharacterEncoding("UTF-8"); 
		 getResponse().setHeader("Cache-Control", "no-cache"); 
		 PrintWriter printout = null;
		 try {
			printout = getResponse().getWriter();
		} catch (IOException e) {
			System.out.println("从Response中得到PrintWriter时错误！");
			System.out.println("出错类路径："+PermissionAction.class);
			e.printStackTrace();
		}		

		String XMLString = this.Permission2XMLString(sonpermission);
		printout.print(XMLString);		
		printout.flush();
		printout.close();
	    System.out.println("XMLString:"+XMLString); 
	    
		return null;
	}

	/**
	 * 把权限解析成XML文件
	 * @return  以字符串的格式返回XML数据
	 */
	public String Permission2XMLString(List<Permission> sonpermlist)
	{
		StringBuilder outstrbr = new StringBuilder(); 
		outstrbr.append("<?xml version='1.0' encoding='UTF-8'?>");
		outstrbr.append("<sonitems>");             //根节点的开始标识
		for (int i=0; i<sonpermlist.size(); i++)
		{
			outstrbr.append("<item>");
			outstrbr.append("<id>"+sonpermlist.get(i).getPermid()+"</id>");
			outstrbr.append("<name>"+sonpermlist.get(i).getPermName()+"</name>");
			if (sonpermlist.get(i).getPermURL() == null)
			{
				outstrbr.append("<url>#</url>");
			}
			outstrbr.append("<url>"+sonpermlist.get(i).getPermURL()+"</url>");

			if (sonpermlist.get(i).getSubPerms().isEmpty()) {
				outstrbr.append("<existson>0</existson>");  //当前子权限菜单不存在自己的子权限菜单
			}
			else 
			{
				outstrbr.append("<existson>1</existson>");  //当前子权限菜单存在自己的子权限菜单
			}
			outstrbr.append("</item>");
		}
		outstrbr.append("</sonitems>");             //根节点的结束标识
		
		return outstrbr.toString();
	}
	
	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public String getParentid() {
		return parentid;
	}
	
}
